package com.ler.yrmall.webapp.service.impl;

import com.ler.yrmall.constant.YuruoConstant;
import com.ler.yrmall.member.api.MemberApi;
import com.ler.yrmall.order.api.OrderApi;
import com.ler.yrmall.webapp.service.PanelService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author Leron
 */
@Service
public class PanelServiceImpl implements PanelService {

    @DubboReference
    private OrderApi orderApi;

    @DubboReference
    private MemberApi memberApi;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Map<String, Object> monthVisited(Integer hover) {
        Map<String,Object> res = new HashMap<>(2);
        List<String> keys = new ArrayList<>();
        List<String> values = new ArrayList<>();
        String hk = YuruoConstant.HOVER_VISITED_PREFIX + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        BoundHashOperations<String, Object, Object> hoverVis = stringRedisTemplate.boundHashOps(hk);
        LocalDateTime now = LocalDateTime.now().minusHours(hover);
        for (int i = 0; i < hover; i++) {
            now = now.plusHours(1);
            keys.add(now.getHour() + "时");
            values.add(String.valueOf(Optional.ofNullable(hoverVis.get(String.valueOf(now.getHour()))).orElse(0)));
        }
        res.put("keys",keys);
        res.put("values",values);
        return res;
    }

    @Override
    public Map<String, Object> dayVisited(Integer day) {
        Map<String,Object> res = new HashMap<>(2);
        List<String> keys = new ArrayList<>();
        List<String> values = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now().minusDays(day);
        for (int i = 0; i < day; i++) {
            now = now.plusDays(1);
            String key = YuruoConstant.DAY_VISITED_PREFIX + now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            keys.add(now.format(DateTimeFormatter.ofPattern("MM-dd")));
            values.add(Optional.ofNullable(stringRedisTemplate.opsForValue().get(key)).orElse("0"));
        }
        res.put("keys",keys);
        res.put("values",values);
        return res;
    }

    @Override
    public Map<String,Object> visited() {
        Map<String,Object> res = new HashMap<>(2);
        String visitedNum = stringRedisTemplate.opsForValue().get(YuruoConstant.VISITED_PREFIX);
        res.put("visitedNum",visitedNum);
        return res;
    }

    @Override
    public Map<String, Object> memberCount() {
        Map<String,Object> res = new HashMap<>(2);
        Long count = memberApi.getMemberCount();
        res.put("memberNum",count);
        return res;
    }

    @Override
    public Map<String, Object> orderCount() {
        Map<String,Object> res = new HashMap<>(2);
        Long count = orderApi.getOrderCount();
        res.put("orderNum",count);
        return res;
    }

    @Override
    public Map<String, Object> tradeAmount() {
        Map<String,Object> res = new HashMap<>(2);
        BigDecimal count = orderApi.getTradeAmount();
        res.put("tradeAmount",count);
        return res;
    }
}
